<template>
	<view class="change_password">
		<view class="class">
			<view class="flex" v-if="!visible">
				验证码已发送到手机号：{{userName}}
			</view>
			<view class="flex" v-if="!visible">
				<label>验证码：</label><input type="text" v-model="code">
			</view>
			<view class="flex" v-if="visible">
				<label>新密码：</label><input type="text" password v-model="newPassword">
			</view>
			<view class="flex" v-if="visible">
				<label>确认密码：</label><input type="text" password v-model="verifyPassword">
			</view>
		</view>
		<button v-if="!visible" type="primary" @click="fuck">点击获取验证码</button>
		<button v-if="!visible" type="primary" @click="handleClickCode">确认</button>
		<button v-else type="primary" @click="handleClick">确认</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				userName: "",
				newPassword: "",
				verifyPassword: "",
				code: "",
				timer: null
			}
		},
		onLoad() {
			this.$http({
				url: '/member/getMyPage'
			}).then(res => {
				if (res.data.code === 200) {
					this.userName = res.data.data.userName
				} else {

				}
			})
		},
		methods: {
			fuck() {
				this.$http({
					url: '/member/sendMdfPayPwdSms'
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: "发送成功",
							icon: "none"
						})
					} else {
						uni.showToast({
							title: "发送失败，请重新进入",
							icon: "none"
						})
					}
				})
			},
			handleClickCode() {
				if (!this.code) {
					uni.showToast({
						title: "请输入验证码",
						icon: "none"
					})
				} else {
					this.$http({
						url: '/member/verificationCode',
						data: {
							memberName: this.userName,
							verificationCode: this.code,
						},
						method: 'POST'
					}).then(res => {
						if (res.data.code === 200) {
							this.visible = true
						} else {
							uni.showToast({
								title: "验证码不正确",
								icon: "none"
							})
						}
					})
				}
			},
			verification() {
				return new Promise((resolve, reject) => {
					if (!this.newPassword) {
						uni.showToast({
							title: "请输入新密码",
							icon: "none"
						})
						reject()
					} else if (!this.verifyPassword) {
						uni.showToast({
							title: "请输入确认密码",
							icon: "none"
						})
						reject()
					} else if (this.newPassword !== this.verifyPassword) {
						uni.showToast({
							title: "两次输入密码不一致",
							icon: "none"
						})
						reject()
					} else {
						resolve()
					}
				})
			},
			handleClick() {
				this.verification().then(resolve => {
					this.$http({
						url: '/member/updLoginPassword',
						data: {
							newPassword: this.newPassword,
							verifyPassword: this.verifyPassword
						},
						method: 'POST'
					}).then(res => {
						if (res.data.code === 200) {
							uni.showToast({
								title: "修改成功",
								icon: "none"
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							uni.showToast({
								title: "修改失败",
								icon: "none"
							})
						}
					})
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	@import './changeOldPassword.css'
</style>
